<!-- 下单界面，收货地址 or 自提门店的选择组件 -->
<template>
  <view class="allAddress" :style="state.isPickUp ? '' : 'padding-top:10rpx;'">
    <view class="nav flex flex-wrap">
      <view
        class="item font-color left-item"
        @tap="switchDeliveryType(1)"
        v-if="deliveryTypes.includes(1)"
        :class="{ active: state.deliveryType === 1 }"
      >快递发货</view>
      <view
        class="item font-color middle-item"
        @tap="switchDeliveryType(2)"
        v-if="deliveryTypes.includes(2)"
        :class="{ active: state.deliveryType === 2 }"
      >用户自提</view>
      <view
        class="item font-color right-item"
        @tap="switchDeliveryType(3)"
        v-if="deliveryTypes.includes(3)"
        :class="{ active: state.deliveryType === 3 }"
      >外卖配送</view>
    </view>
    <!-- 情况一：收货地址的选择  1:快递发货 3:外卖配送 -->
    <view
      class="address flex flex-wrap flex-center ss-row-between"
      @tap="onSelectAddress"
      v-if="state.deliveryType == 1 || state.deliveryType == 3"
      :style="state.isPickUp ? '' : 'border-top-left-radius: 14rpx;border-top-right-radius: 14rpx;'"
    >
      <view class="addressCon" v-if="state.addressInfo.name">
        <view class="name"
          >{{ state.addressInfo.name }}
          <text class="phone">{{ state.addressInfo.mobile }}</text>
        </view>
        <view class="flex flex-wrap">
          <text class="default font-color" v-if="state.addressInfo.defaultStatus">[默认]</text>
          <text class="line2">
            {{ state.addressInfo.areaName }} {{ state.addressInfo.detailAddress }}
          </text>
        </view>
      </view>
      <view class="addressCon" v-else>
        <view class="setaddress">设置收货地址</view>
      </view>
      <view class="iconfont">
        <view class="ss-rest-button">
          <text class="_icon-forward" />
        </view>
      </view>
    </view>
    <!-- 情况二：门店的选择 2:自提 -->
    <view
      class="address flex flex-wrap flex-center ss-row-between"
      v-if="state.deliveryType == 2"
      @tap="onSelectAddress"
    >
      <view class="addressCon" v-if="state.pickUpInfo.name">
        <view class="name"
          >{{ state.pickUpInfo.name }}
          <text class="phone">{{ state.pickUpInfo.phone }}</text>
        </view>
        <view class="line1">
          {{ state.pickUpInfo.areaName }}{{ ', ' + state.pickUpInfo.detailAddress }}
        </view>
      </view>
      <view class="addressCon" v-else>
        <view class="setaddress">选择自提门店</view>
      </view>
      <view class="iconfont">
        <view class="ss-rest-button">
          <text class="_icon-forward" />
        </view>
      </view>
    </view>
    <!-- 自提类型  1堂食 2外带 -->
    <view class="address flex  align-center" v-if="state.deliveryType == 2">
      <view class="">自提方式:</view>
      <radio-group @change="radioChange" class="flex align-center ml-4">
        <label class="flex align-center uni-list-cell uni-list-cell-pd mr-4" v-for="item in selfPickupList" :key="item.value">
					<view>
						<radio :value="item.value" :checked="item.value === state.pickType" style="transform:scale(0.9)" color="#007AFF"/>
					</view>
					<view>{{item.name}}</view>
				</label>
      </radio-group>
    </view>
    <view class="line">
      <image :src="sheep.$url.static('/static/images/line.png', 'local')" />
    </view>
  </view>
</template>

<script setup>
  import { computed, watch } from 'vue';
  import sheep from '@/sheep';
  import { isEmpty } from 'lodash-es';
  import DeliveryApi from '@/sheep/api/trade/delivery';

  const props = defineProps({
    modelValue: {
      type: Object,
      default() {},
    },
    deliveryTypes: {
      type: Array,
      default() {},
    },
  });
  const emits = defineEmits(['update:modelValue']);

  // computed 解决父子组件双向数据同步
  const state = computed({
    get() {
      return new Proxy(props.modelValue, {
        set(obj, name, val) {
          emits('update:modelValue', {
            ...obj,
            [name]: val,
          });
          return true;
        },
      });
    },
    set(val) {
      emits('update:modelValue', val);
    },
  });

  // 选择地址
  function onSelectAddress() {
    let emitName = 'SELECT_ADDRESS';
    let addressPage = '/pages/user/address/list?type=select';
    if (state.value.deliveryType === 2) {
      emitName = 'SELECT_PICK_UP_INFO';
      addressPage = '/pages/user/goods_details_store/index';
    }
    uni.$once(emitName, (e) => {
      changeConsignee(e.addressInfo);
    });
    sheep.$router.go(addressPage);
  }

  // 更改收货人地址&计算订单信息
  async function changeConsignee(addressInfo = {}) {
    if (!isEmpty(addressInfo)) {
      if (state.value.deliveryType === 1) {
        state.value.addressInfo = addressInfo;
      }
      if (state.value.deliveryType === 2) {
        state.value.pickUpInfo = addressInfo;
      }
    }
  }

  // 收货方式切换
  const switchDeliveryType = (type) => {
    state.value.deliveryType = type;
    if (type == 2) {
      init(type);
    }
  };

  //默认选中收获地址
  async function init(type) {
    if (type === 2) {
      const { data, code } = await DeliveryApi.getDeliveryPickUpStoreList();
      if (code !== 0) {
        return;
      }
      let d = data.find((item) => item.id === sheep.$store('selectStore').currentSelect.id);
      changeConsignee(d);
    }
  }

  watch(state.value, (val) => {
    if (val.deliveryType === 2) {
      init(val.deliveryType);
    }
  });

  //自提逻辑
  const selfPickupList = [
    {
      name: '堂食',
      value: 1,
    },
    {
      name: '外带',
      value: 2,
    },
  ];
  const radioChange = (e) => {
    state.value.pickType = e.detail.value;
  };
</script>

<style scoped lang="scss">
  .allAddress .font-color {
    color: #e93323 !important;
  }
  .line2 {
    width: 504rpx;
  }
  .textR {
    text-align: right;
  }

  .line {
    width: 100%;
    height: 3rpx;
  }

  .line image {
    width: 100%;
    height: 100%;
    display: block;
  }

  .address {
    padding: 28rpx;
    background-color: #fff;
    box-sizing: border-box;
  }

  .address .addressCon {
    width: 596rpx;
    font-size: 26rpx;
    color: #666;
  }

  .address .addressCon .name {
    font-size: 30rpx;
    color: #282828;
    font-weight: bold;
    margin-bottom: 10rpx;
  }

  .address .addressCon .name .phone {
    margin-left: 50rpx;
  }

  .address .addressCon .default {
    margin-right: 12rpx;
  }

  .address .addressCon .setaddress {
    color: #333;
    font-size: 28rpx;
  }

  .address .iconfont {
    font-size: 35rpx;
    color: #707070;
  }

  .allAddress {
    width: 100%;
    background: linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
    // background-image: linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
    // background-image: -webkit-linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
    // background-image: -moz-linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
    //padding: 100rpx 30rpx 0 30rpx;
    padding-top: 60rpx;
    padding-bottom: 10rpx;
  }

  .allAddress .nav {
  width: 690rpx;
  margin: 0 auto;
  display: flex;
  overflow: hidden;
  position: relative;
  // 消除行内元素间隙
  font-size: 0;
}

.allAddress .nav .item {
  position: relative;
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  font-size: 28rpx;
  z-index: 1;
  color: #333;
  background-color: #fff;
  border-radius: 14rpx 0 0 0;
  margin-right: -15rpx; /* 负边距使元素重叠 */
  overflow: hidden;
}

/* 左侧梯形 - 左侧垂直，右侧向右倾斜 */
.allAddress .nav .left-item {
  clip-path: polygon(0 0, 100% 0, calc(100% - 20rpx) 100%, 0 100%);
  z-index: 3;
  margin-right: -20rpx;
}

/* 中间平行四边形 - 两侧都向右倾斜 */
.allAddress .nav .middle-item {
  clip-path: polygon(20rpx 0, 100% 0, calc(100% - 20rpx) 100%, 0 100%);
  z-index: 2;
}

/* 右侧梯形 - 左侧向右倾斜，右侧垂直 */
.allAddress .nav .right-item {
  clip-path: polygon(20rpx 0, 100% 0, 100% 100%, 0 100%);
  border-radius: 0 14rpx 0 0;
  z-index: 1;
  margin-left: -20rpx;
}

/* 选中状态样式 */
.allAddress .nav .item.active {
  background-color: #f7c1bd;
  color: white;
  z-index: 4;
}


  .allAddress .address {
    width: 690rpx;
    max-height: 180rpx;
    margin: 0 auto;
  }

  .allAddress .line {
    width: 100%;
    margin: 0 auto;
  }
</style>
